﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.Customer.DAL.XMLStorage
{
	public class WorkAreaDAL : BaseStorageDAL, IWorkAreaDAL
	{

		#region IWorkAreaDAL Members

		public void AddWorkArea(WorkArea workarea, IDbTransaction trans)
		{
			dtoXmlStorage.WorkAreaRow workAreaRow = Storage.WorkArea.FindById(workarea.Id);

			bool isNew = (workAreaRow == null);

			if (isNew)
			{
				workAreaRow = Storage.WorkArea.NewWorkAreaRow();
			}

			workAreaRow.Id = workarea.Id;
			workAreaRow.Name = workarea.Name;

			
				workAreaRow.ParentId = workarea.ParentId;

			if (isNew)
			{
				Storage.WorkArea.AddWorkAreaRow(workAreaRow);
			}

			if (trans == null)
			{
				AcceptChanges();
			}

		}



		public WorkArea ReadWorkAreaById(string id)
		{
			dtoXmlStorage.WorkAreaRow workarea = Storage.WorkArea.FindById(id);

			if (workarea == null)
				return null;

			WorkArea result = new WorkArea();
			result.Id = workarea.Id;
			result.Name = workarea.Name;
			if (!workarea.IsParentIdNull())
				result.ParentId = workarea.ParentId;

			return result;
		}

		public WorkArea ReadWorkAreaByName(string name)
		{
			foreach (dtoXmlStorage.WorkAreaRow wr in Storage.WorkArea)
			{
				if (string.Compare(name, wr.Name, true) == 0)
				{
					return ReadWorkAreaById(wr.Id);
				}
			}

			return null;
		}



		#endregion
	}
}
